import { BaseEntity } from '@libs/common/entities';
import { ApiProperty } from '@nestjs/swagger';
import { Column, Entity, Index, JoinColumn } from 'typeorm';

import { PointType } from '@/constant.enum';
import { Order } from '@/market/order/entities/order.entity';

@Index('idx_crm_node_market_points_history_orderNo_type', ['orderNo', 'type'])
@Entity('crm_node_market_points_history')
export class PointsHistory extends BaseEntity {
  @ApiProperty({ description: '目标用户 ID' })
  @Column({ comment: '目标用户 ID' })
  userId: string;

  @ApiProperty({ description: '用户名称' })
  @Column({ comment: '用户名称' })
  userName?: string;

  @ApiProperty({ description: '操作用户 ID' })
  @Column({ comment: '操作用户 ID', nullable: true })
  operatorId?: string;

  @ApiProperty({ description: '操作用户名称' })
  @Column({ comment: '操作用户名称', nullable: true })
  operatorName?: string;

  @ApiProperty({
    description:
      '积分变动的类型，performance(绩效)、attendance(考勤)、refund(退款)、manual(手动)、consume(消费)',
  })
  @Column({
    type: 'enum',
    enum: PointType,
    comment:
      '积分变动的类型，performance(绩效)、attendance(考勤)、refund(退款)、manual(手动)、consume(消费)',
  })
  type: (typeof PointType)[number];

  @ApiProperty({ description: '积分的变动数量' })
  @Column('double', { comment: '积分的变动数量' })
  points: number;

  @JoinColumn({ name: 'order' })
  order?: Order;

  @Column({
    comment: '关联到积分交易表中的交易ID',
    nullable: true,
    default: null,
  })
  orderNo?: string;

  @ApiProperty({ description: '说明' })
  @Column({ comment: '说明', nullable: true })
  desc?: string;
}
